<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentEventComments.class.php, $Author:  $ */



/** Component for showing comments to events
 * @version $Id: PhpFox_ComponentEventComments.class.php 422 2008-04-01 13:30:10Z phpfoxsv $
 * @package module.event
 *   
 */
class PhpFox_Mod_Event_ComponentEventComments extends BaseComponent
{
    function process()
    {
        $oEventInviteItem = &$this->_oModule->getItem('EventInvite');
        /* @var $oEventInviteItem PhpFox_Mod_Event_ItemEventInvite */
        $oEventItem = &$this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = &$this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        App::loadClass('Pager');

        $oSecurityService->checkPerm('events', array('read_only', 'full'));
        $oSecurityService->checkAccess('events');
        $iEventId = $oReq->getInt('id');
        if (!$iEventId)
        {
            App::gotoUrl('public.event.main');
        }
        if (!$oEventItem->load($iEventId))
        {
            App::gotoUrl('public.event.main');
        }
        $sUser = $oSecurityService->getCurrentUserLogin();
        if ($iCommentId = $oReq->getInt('cdelete'))
        {
            if (!$oEventInviteItem->load($iCommentId))
            {
                App::gotoUrl('public.event.main');
            }
            if ($oEventInviteItem->aData['event_id'] != $iEventId)
            {
                App::gotoUrl('public.event.main');
            }
            $bCanDelete = false;
            if ($oSecurityService->isStaff())
            {
                $bCanDelete = true;
            }
            elseif ($oEventItem->aData['user'] == $sUser)
            {
                $bCanDelete = true;
            }
            elseif ($oEventInviteItem->aData['user'] == $sUser)
            {
                $bCanDelete = true;
            }
            if ($bCanDelete && $oSecurityService->checkPerm('events', 'full', false))
            {
                $oEventInviteItem->delete($iCommentId);
            }
            App::gotoUrl('public.event.view', array('id' => $iEventId));
        }
        $iPageSize = App::getParam('total_page_comments');
        $iPage = $oReq->getInt('page', 1);
        $aCond = array();
        $aCond['event_id'] = '= '.$iEventId;
        $aCond[]           = '(({#type} = 0 AND {#invite} <> 0) OR ({#type} <> 0 AND {#invite} = 0))';
        $sSort = 'time DESC';
        list($aItems, $iCnt) = $oEventInviteItem->getList($aCond, $iPage, $iPageSize, $sSort);
		foreach ($aItems as $iKey => $aItem)
		{
		    $bCanDelete = false;
		    if ($oSecurityService->isStaff())
		    {
		        $bCanDelete = true;
		    }
		    elseif ($aItem['user'] == $sUser)
		    {
		        $bCanDelete = true;
		    }
		    elseif ($oEventItem->aData['user'] == $sUser)
		    {
		        $bCanDelete = true;
		    }
		    $aItems[$iKey]['can_delete'] = ($bCanDelete && $oSecurityService->checkPerm('events', 'full', false));
		}
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);

        $oTpl->assignSrc('aItems', $aItems);
        $oTpl->assign(array(
        	'bShowImages' => App::getSetting('show_comment_images'),
        	'aPaging'     => $oPager->getInfo($oUrl),
        	'sUImageUrl'  => App::getParam('user.pic.url'),
        ));
        return $oTpl->fetch('EventComments.html');
    }
}
?>